﻿(function () {
    "use strict";

    var projects = new WinJS.Binding.List();
    
    WinJS.UI.Pages.define("/pages/calendar/weekDetailCalendar/weekDetailCalendar.html", {
        ready: function (element, options) {
            
            DateUtility.CurrentDay = options.day;

            initPage();

            var day0 = document.querySelector("#day0");

            document.querySelector("#firstSubHeader").addEventListener("click", function () {
                var date = new Date(DateUtility.CurrentYear, DateUtility.CurrentMonth, DateUtility.CurrentDay).addDays(-7);
                DateUtility.CurrentDay = date.getDate();
                DateUtility.CurrentMonth = date.getMonth();
                DateUtility.CurrentYear = date.getFullYear();
                initPage();
            });
            document.querySelector("#thirdSubHeader").addEventListener("click", function () {
                var date = new Date(DateUtility.CurrentYear, DateUtility.CurrentMonth, DateUtility.CurrentDay).addDays(7);
                DateUtility.CurrentDay = date.getDate();
                DateUtility.CurrentMonth = date.getMonth();
                DateUtility.CurrentYear = date.getFullYear();
                initPage();
            });
        }
    });

    var initPage = function () {
        var date = new Date(DateUtility.CurrentYear, DateUtility.CurrentMonth, DateUtility.CurrentDay);
        var day = date.getDay() - 1;
        if (day == -1) day = 6;

        var firstDateOfWeek = new Date(date);
        firstDateOfWeek.addDays(-day);
        var lastDateOfWeek = new Date(firstDateOfWeek);
        lastDateOfWeek.addDays(6);

        document.querySelector("#secondSubHeader").innerText = firstDateOfWeek.toString("d MMMM") + " - " + lastDateOfWeek.toString("d MMMM");

        var i = 0;
        day = new Date(firstDateOfWeek);
        for (i = 0; i < 5; i++) {
            document.querySelector("#day" + eval(i)).innerText = day.toString("dddd d/M");
            document.querySelector("#day" + eval(i)).innerText = document.querySelector("#day" + eval(i)).innerText.replace(new RegExp("^(.)","g"), function (a, x) { return a.replace(x, x.toUpperCase()); });
            day.addDays(1);
        }

        // bouchon
        projects.splice(0, projects.length);
        var projetBouchon = { projectName: "ADP", projectID: 1 };
        projetBouchon.MondayList = new WinJS.Binding.List();
        projetBouchon.MondayList.push({ toto: "toto" });
        projetBouchon.MondayList.push({ toto: "toto" });
        
        projects.push(projetBouchon);
    }

    var MainItemTemplate = function (itemPromise) {
        return itemPromise.then(function (currentItem) {

            var result = document.createElement("div");

            var body = document.createElement("div");
            $(body).addClass("itemListView");

            var title =
                "<div class=\"headerItemListView\">" +
                    "<div>" + currentItem.data.projectName + "</div>" +
                    "<div class=\"hidden\"><div>" + currentItem.data.projectID + "</div></div>" +
                "</div>";

            $(body).append(title);

            var content = $("<div class=\"itemListViewDay0\"></div>");
            
            var nestedListView = document.createElement("div");
            nestedListView.className = "SubListView"
            
            /*var listView = new WinJS.UI.ListView(nestedListView);
            listView.itemTemplate = document.getElementById("subTemplateListView");
            listView.itemDataSource = currentItem.data.MondayList.dataSource;

            $(content).append($(nestedListView));*/
            $(body).append(content);    
            
            result.appendChild(body);
            return result;

        });
    }

    
    var publicMembres = {
        projects: projects,
        MainItemTemplate : WinJS.Utilities.markSupportedForProcessing(MainItemTemplate)
    };


    WinJS.Namespace.define("WeekDetailCalendar", publicMembres);
    
})();
